{include file="common/header"/}

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
              <div class="layui-card-body">
                <form  lay-filter="form" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <label class="layui-form-label">字段类型</label>
                        <div class="layui-input-inline">
                            <select id="type" name="type" lay-filter="type" class="required" lay-verify="required" {if condition="ACTION_NAME eq 'fieldedit'"}disabled{/if}>
                                <option value='' >请选择字段类型</option>
                                <option value="catid">栏目</option>
                                {if condition="ACTION_NAME eq 'fieldedit'"}<option value="title">标题</option>{/if}
                                <option value="text" >单行文本</option>
                                <option value="textarea" >多行文本</option>
                                <option value="editor" >编辑器</option>
                                <option value="select" >下拉列表</option>
                                <option value="radio" >单选按钮</option>
                                <option value="checkbox" >复选框</option>
                                <option value="image" >单张图片</option>
                                <option value="images" >多张图片</option>
                                <option value="file" >文件上传</option>
                                <option value="number" >数字</option>
                                <option value="datetime" >日期和时间</option>
                                <option value="groupid" >会员组</option>
                                <option value="linkage" >地址联动</option>
                                <option value="template">模板选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">字段名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="field" lay-verify="required" placeholder="必填：字段名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">别名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="name" lay-verify="required" placeholder="必填：别名" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">字段设置</label>
                        <div class="layui-input-inline" id="field_setup">

                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">class名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="class" value="" placeholder="class名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">必填</label>
                        <div class="layui-input-block">
                            <input type="radio" name="required" value="1" title="是">
                            <input type="radio" name="required" value="0" checked title="否">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">验证规则</label>
                        <div class="layui-input-inline">
                            <select name="pattern" lay-verify="required">
                                <option value='' >请选择字段类型</option>
                                {volist name='field_pattern' id="list"}
                                <option value='{$list.name}' >{$list.title}</option>
                                {/volist}
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">字符长度</label>
                        <div class="layui-input-inline" style="width: 100px;min-width: 100px;">
                            <input type="text" name="minlength" class="layui-input">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline" style="width: 100px;min-width: 100px;">
                            <input type="text" name="maxlength" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">错误信息</label>
                        <div class="layui-input-inline">
                            <input type="text" name="errormsg" value="" placeholder="验证失败错误信息" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" lay-submit="" lay-filter="filedSubmit">{:lang('submit')}</button>
                        </div>
                    </div>
                </form>

              </div>
            </div>
        </div>
    </div>
</div>
<div class="admin_main">
    <div class="admin_base" ng-app="hd" ng-controller="ctrl">
        <div class="box_form">
            <div class="box_content">
                
            </div>
        </div>
    </div>


</div>
<script>
layui.use(['form'], function () {
    var form = layui.form;
    var field = '{$info|raw}'!='null'?{$info|raw}:{type:'',field:'',name:'',required:1,minlength:'',maxlength:'',errormsg:'',pattern:''};
    var pattern = {$pattern|raw};
    var oldfield = field;
    field_setting(field,'edit',form);

    form.on('select(type)', function(type){
        field_setting(type,'add',form);
    });
    form.on('submit(filedSubmit)', function (data) {
        var loading = layer.load(1, {shade: [0.1, '#fff']});
        data.field.moduleid = "{$moduleid}";
        if(field.id){
            data.field.oldfield = oldfield;
            data.field.id = field.id;
        }
        $.post("", data.field, function (res) {
            layer.close(loading);
            if (res.code > 0) {
                layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                    window.parent.location.href = res.url;
                });
            } else {
                layer.msg(res.msg, {time: 1800, icon: 2});
            }
        });
    });
    var info = '';
    info = {$info|raw};
    form.val("form", info);
    form.render();
    
});

function field_setting(info,action,form) {
    if(action=='add'){
        var data =  '';
        var url =  "{:url('fieldAdd')}?isajax=1&moduleid={$moduleid}&type="+type.value;
    }else{
        var data = info.setup;
        var url =  "{:url('fieldAdd')}?isajax=1&moduleid={$moduleid}&type="+info.type+"&name="+info.field;
    }
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        beforeSend:function(){
            $('#field_setup').html('<i class="fa fa-spinner fa-spin fa-fw"></i>');
            form.render()
        },
        success: function(msg){
            $('#field_setup').html(msg);
            form.render()
        },
        complete:function(){
        },
        error:function(){
        }
    });
}
</script>